import { createDom, createTitle } from '../utils'
import {
  getUrlParams,
  objectToUrlParams,
  preserveDecimalLaces,
  copyText,
  fileToBase64,
  fileToBlob,
  fileToArrayBuffer,
  base64ToBlob,
  base64ToFile,
  base64ToArrayBuffer,
  blobToFile,
  blobToUrl,
} from '../../lib/index'

createTitle('一般工具函数')

// 取URL参数
;(function () {
  const p = createDom('p', ['取URL参数'], {}, false)
  createDom('div', [p], { class: 'item' })
  console.log(getUrlParams(), 'getUrlParams')
})()

// 对象转URL参数格式
;(function () {
  const p = createDom('p', ['对象转URL参数格式'], {}, false)
  createDom('div', [p], { class: 'item' })
  const obj = {
    name: 'zs',
    age: 1,
  }
  console.log(objectToUrlParams(obj, true), 'objectToUrlParams')
})()

// 数字保留小数位数
;(function () {
  const p = createDom('p', ['数字保留小数位数'], {}, false)
  const input: any = createDom('input', [''], { type: 'text' }, false)
  const button = createDom('button', ['验证'], {}, false)
  createDom('div', [p, input, button], { class: 'item' })
  button.onclick = () => {
    const num = preserveDecimalLaces(input.value, 2, false, false)
    console.log(num, 'preserveDecimalLaces')
  }
})()

// 复制文本到剪切板
;(function () {
  const p = createDom('p', ['复制文本到剪切板'], {}, false)
  const input: any = createDom('input', [''], { type: 'text' }, false)
  const button = createDom('button', ['复制'], {}, false)
  createDom('div', [p, input, button], { class: 'item' })
  button.onclick = () => {
    copyText(input.value).then((res) => {
      console.log(res, 'copyText')
    })
  }
})()

// file对象转换
;(function () {
  const p = createDom('p', ['file对象转换'], {}, false)
  const input: any = createDom('input', [''], { type: 'file' }, false)
  createDom('div', [p, input], { class: 'item' })
  input.onchange = (e) => {
    const f = e.target.files[0]
    console.log(f, '原始文件')

    // 转Base64
    // fileToBase64(f).then(({ baseUrl, atobUrl }) => {
    //   console.log(baseUrl, 'baseUrl')
    //   // base64转file
    //   // const base64ToFileRes = base64ToFile(baseUrl)
    //   // console.log(base64ToFileRes, 'base64转file')
    //   // // base64转Blob
    //   // const base64ToBlobRes = base64ToBlob(baseUrl)
    //   // console.log(base64ToBlobRes, 'base64转Blob')
    //   // // base64转ArrayBuffer
    //   // const attrBuff = base64ToArrayBuffer(atobUrl)
    //   // console.log(attrBuff, 'base64转ArrayBuffer')
    // })

    // 转Blob
    fileToBlob(f).then((res) => {
      console.log(res, 'fileToBlob')
      // Blob转file
      const blobToFileRes = blobToFile(res.blob, '123.mp3')
      console.log(blobToFileRes, 'blobToFileRes')
      // Blob转url
      blobToUrl(res.blob).then((res) => {
        createDom('video', [], { src: res.blobUrl, controls: true })
      })
    })
    // 转ArrayBuffer
    // fileToArrayBuffer(f).then((res) => {
    //   console.log(res, 'fileToArrayBuffer')
    // })

    // fileToBlob, fileToArrayBuffer
  }
})()
